#include<iostream>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int n;
int main()
{
	int t;
	cin >> t;
	while (t--) {
		LL n, m;
		scanf("%lld%lld", &n, &m);
		LL k = n - m;
		if (m == n) {
			printf("%lld\n", 4 * ((n + 1) / 2));
			continue;
		}
		if (k >= m) {
			printf("%lld\n", 4 * m);
		}
		else {
			printf("%lld\n", 4 * k + 4 * (((m - k) + 1) / 2));
		}
	}
	
	return 0;
}